package com.yunhe.rentcars.pro.service.car.impl;

import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.constant.Constants;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.dao.car.CarRepairDao;
import com.yunhe.rentcars.pro.dao.car.CarsDao;
import com.yunhe.rentcars.pro.entity.car.CarRepair;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author maliang
 * @version V1.0
 * @date 2017/11/25 0025 15:34
 * @Description: 车辆维修业务层
 */
@Service
public class CarRepairServiceImpl implements CarRepairService {

    @Autowired
    private CarRepairDao carRepairDao;
    @Autowired
    private CarsDao carsDao;

    @Override
    @Log(value = "添加车辆维修记录",type = 1)
    public void save(CarRepair carRepair) {
        carstate(carRepair);
        carRepairDao.save(carRepair);
    }

    @Override
    @Log(value = "删除维修记录",type = 2)
    public void delBatch(String ids) {
        String[] split = ids.split(",");
        carRepairDao.delBatch(split);
    }

    @Override
    @Log(value = "修改维修记录",type = 3)
    public void update(CarRepair carRepair) {
        carstate(carRepair);
        carRepairDao.update(carRepair);
    }

    @Override
    public Pageable<CarRepair> find(CarRepair carRepair, Integer offset, Integer limit) {
        Pageable<CarRepair> pageable = new Pageable<>();
        Long count = carRepairDao.findCount(carRepair);
        List<CarRepair> rows = null;
        if(count > 0){
            rows = carRepairDao.findList(carRepair.getCarCode(), carRepair.getUsecarTime(), carRepair.getReturnTime(), offset, limit);
        }
        pageable.setTotal(count.intValue());
        pageable.setRows(rows);
        return pageable;
    }

    @Override
    public CarRepair findOne(Long recordid) {
        return carRepairDao.findOne(recordid);
    }

    /**
     * 更改车辆信息
     * @param carRepair
     */
    public void carstate(CarRepair carRepair){
        Date returnTime = carRepair.getReturnTime();
        Cars cars = new Cars();
        cars.setCarcode(carRepair.getCarCode());
        if(returnTime != null){
            carRepair.setStatus(Constants.CAR_NORMAL);
            cars.setCarstate(Constants.CAR_NORMAL);
        }else{
            cars.setCarstate(Constants.CAR_IN_REPAIR);
        }
        carsDao.carState(cars);
    }
}
